<template>
	<zero-custom-bar :effect="false" color="#fff" backgroundColor="linear-gradient(180deg, #FE9B23 0%, #FEA71D 100%)">
		<template v-slot:left>
			<view class="search-box">
				<view class="left" @click="back">
					<uni-icons color="#fff" type="back" size="22"></uni-icons>
				</view>
				<view class="search">
					<view class="city" @click="onCityAll">
						<text class="slh">{{queryParams.city}}</text>
						<image src="../../static/bot.png" mode=""></image>
					</view>
					<view class="inp">
						<image src="../../static/search.png" mode=""></image>
						<input type="text" placeholder="请输入门店名称" v-model="queryParams.storeName" @confirm="getData" />
					</view>
				</view>
			</view>
		</template>
	</zero-custom-bar>
	<view class="distance">
		<!-- <view class="city">
			<text>全市</text>
			<image src="../../static/bot.png" mode=""></image>
		</view> -->
		<text>距离优先</text>
	</view>
	<view class="tabs">
		<view class="tab" v-for="(item,index) in tabList" :key="index" @click="handleTab(item,index)">
			<image :src="tabIndex==index ?'/static/wash1.png':'/static/wash1-1.png'" v-if="index==0" mode=""></image>
			<image :src="tabIndex==index ?'/static/wash2.png':'/static/wash2-2.png'" v-if="index==1" mode=""></image>
			<image :src="tabIndex==index ?'/static/wash3.png':'/static/wash3-3.png'" v-if="index==2" mode=""></image>
			<image :src="tabIndex==index ?'/static/wash4.png':'/static/wash4-4.png'" v-if="index==3" mode=""></image>
			<image :src="tabIndex==index ?'/static/wash5.png':'/static/wash5-5.png'" v-if="index==4" mode=""></image>
			<text>{{item.name}}</text>
		</view>

	</view>
	<view class="list">
		<view class="item" v-for="(item,index) in storeList" :key="index" @click="bindStoreInfo(item)">
			<view class="header">
				<image :src="item.outPic" mode=""></image>
				<view class="">
					<text class="name slh">{{item.storeName}}</text>
					<text class="address slh">{{item.address}}</text>
					<view class="cale">
						<image src="/static/feiji.png" mode=""></image>
						<text>{{item.distance}}km</text>
					</view>
				</view>

			</view>
			<view class="bottom" v-for="(item,index) in item.productInfoList" :key="index">
				<view class="left">
					<view class="">
						<image src="/static/car-icon.png" mode=""></image>
						<text>{{item.name}}</text>
					</view>
					<text>全城合作门店都可用 </text>
				</view>
				<view class="right">
					<view class="">
						<text class="unamount">￥{{item.initPrice/100}}</text>
						<text class="amount">￥{{item.salePrice/100}}</text>
					</view>
					<button class="btn" @click="onCarWash(item)">立即付款</button>
				</view>
			</view>
		</view>
		<uni-empty v-if="emptyState"></uni-empty>
	</view>
</template>

<script setup>
	import {
		washStoreList,
		getCity,
		getManyProductInfo
	} from '@/api'
	import {
		onLoad,
		onReachBottom,
		onShow,
	} from '@dcloudio/uni-app'
	import {
		ref
	} from 'vue';
	const emptyState =ref(false)
	onLoad(async () => {

		getData()
		const tabs = await getManyProductInfo({
			businessType: "2"
		})
		tabList.value = tabs.result
		getDefaultCity()
		uni.$on('city',item=>{
			queryParams.value.city = item.areaName
			queryParams.value.cityCode = item.areaCode
			getData()
		})
	})
	const storeList = ref([])
	const tabIndex = ref(0)
	const queryParams = ref({
		storeName: "",
		lat: "",
		lot: "",
		businessType: "2",
		productInfoId: "",
		cityCode: "",
		city: ""
	})
	const tabList = ref()
	/** 门店列表 */
	const getData = async () => {
		uni.getLocation({
			type: 'wgs84',
			success: async (res) => {
				queryParams.value.lat = res.latitude
				queryParams.value.lot = res.longitude
				const store = await washStoreList(queryParams.value)
				storeList.value = store.result
				if(storeList.value.length==0){
					emptyState.value = true
				}else{
					emptyState.value = false
				}
			},
			fail: (err) => {
				uni.showModal({
					title: '位置授权提示',
					content: '打开定位失败，您可以检查手机定位是否开启后手动进行位置授权'
				})
			}
		});

	}
	/** 获取默认城市*/
	const getDefaultCity = () => {
		uni.getLocation({
			type: 'wgs84',
			success: async (res) => {
				const city = await getCity({
					lng: res.longitude,
					lat: res.latitude
				})
				queryParams.value.city = city.result.cityName
				queryParams.value.cityCode = city.result.cityCode

			},
			fail: (err) => {
				uni.showModal({
					title: '位置授权提示',
					content: '打开定位失败，您可以检查手机定位是否开启后手动进行位置授权'
				})
			}
		});

	}
	const back = () => {
		uni.navigateBack()
	}
	/** 门店详情 */
	const bindStoreInfo = (item) => {
		uni.navigateTo({
			url: "/pages/storeInfo/storeInfo?id=" + item.id
		})
	}
	/** 根据明细筛选*/
	const handleTab = (item,index) => {
		tabIndex.value = index
		queryParams.value.productInfoId = item.id
		getData()
	}
	/** 查询全部城市 */
	const onCityAll = ()=>{
		uni.navigateTo({
			url:'/pages/cityAll/cityAll'
		})
	}

	const onCarWash = (item) => {
		uni.setStorageSync('carWashPay', item)
		uni.navigateTo({
			url: '/pages/carWashPay/carWashPay'
		})
	}
</script>

<style lang="scss">
	page {
		background: #F8F5F0;
		padding-bottom: constant(safe-area-inset-bottom);
		/*兼容 IOS<11.2*/
		padding-bottom: env(safe-area-inset-bottom);
		/*兼容 IOS>11.2*/
	}

	.search-box {
		display: flex;

		.left {
			margin-left: 26rpx;
			margin-right: 30rpx;
		}

		.search {
			height: 58rpx;
			min-width: 440rpx;
			background: #fff;
			border-radius: 30rpx;
			margin-right: 80rpx;
			display: flex;
			align-items: center;

			.city {
				display: flex;
				align-items: center;
				width: 160rpx;
				justify-content: center;
				border-right: 2rpx solid #E5E5E5;
				height: 36rpx;

				text {
					font-size: 26rpx;
					color: #16191B;
					width: 120rpx;
					text-align: center;
				}

				image {
					display: block;
					width: 18rpx;
					height: 18rpx;
				}
			}

			.inp {
				display: flex;
				align-items: center;

				image {
					display: block;
					width: 24rpx;
					height: 24rpx;
					margin-left: 14rpx;
					margin-right: 14rpx;
				}

				input {
					font-size: 26rpx;
					flex: 2;
				}
			}
		}
	}

	.distance {
		display: flex;
		align-items: center;
		width: 700rpx;
		margin: 38rpx auto 57rpx;

		// .city{
		// 	display: flex;
		// 	align-items: center;
		// 	text{
		// 		font-size: 32rpx;
		// 		color: #16191B;
		// 	}
		// 	image{
		// 		display: block;
		// 		width: 18rpx;
		// 		height: 18rpx; 
		// 		margin-left: 5rpx;
		// 	}
		// }
		>text {
			font-weight: 400;
			font-size: 28rpx;
			color: #FE9B23;
			// margin-left: 50rpx;
		}
	}

	.tabs {
		display: flex;
		width: 700rpx;
		margin: 30rpx auto;
		justify-content: space-between;

		.tab {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			image {
				display: block;
				width: 100rpx;
				height: 77rpx;
				margin-bottom: 12rpx;
			}

			text {
				font-size: 30rpx;
				color: #16191B;
			}
		}
	}

	.list {
		margin: 40rpx auto;

		.item {
			background: #FFFFFF;
			border-radius: 36rpx;
			margin: 0 auto 16rpx;
			padding: 36rpx;
			width: 630rpx;

			.header {
				width: 630rpx;
				display: flex;
				align-items: flex-start;

				>image {

					display: block;
					width: 150rpx;
					height: 150rpx;
					margin-right: 18rpx;
				}

				>view {
					.name {
						color: #16191B;
						font-size: 30rpx;
						margin-top: 8rpx;
						width: 360rpx;
					}

					.address {

						color: #8E8E8E;
						font-size: 26rpx;
						width: 360rpx;
						margin-top: 10rpx;
					}

				}

				.cale {
					display: flex;
					align-items: center;
					margin-top: 20rpx;

					image {
						display: block;
						width: 26rpx;
						height: 26rpx;
						margin-right: 8rpx;
					}

					text {
						font-size: 26rpx;
						color: #FE9B23;
					}

				}
			}

			.bottom {
				margin-top: 30rpx;
				width: 558rpx;

				background: #F6F6F6;
				border-radius: 21rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 32rpx 36rpx;
				overflow: hidden;

				.left {

					>view {
						display: flex;
						align-items: center;

						image {
							width: 54rpx;
							height: 54rpx;
							display: block;
							margin-right: 8rpx;
						}

						text {
							color: #16191B;
							font-size: 30rpx;
						}
					}

					>text {
						color: #8E8E8E;
						font-size: 26rpx;
						margin-top: 15rpx;
						display: block;
					}
				}

				.right {

					view {
						margin-bottom: 22rpx;

						.unamount {
							font-size: 26rpx;
							color: #8E8E8E;
							margin-right: 12rpx;
							text-decoration: line-through;
						}

						.amount {
							font-weight: bold;
							font-size: 30rpx;
							color: #ED4538;
						}
					}

					.btn {
						width: 160rpx;
						height: 70rpx;
						background: #FE9B23;
						border-radius: 35rpx;
						font-size: 26rpx;
						color: #FEFFFF;
						line-height: 70rpx;
					}
				}
			}
		}
	}
</style>